package gui.standard.form;

import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.tablemodel.MagacinTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JLimitedTextField;
import actions.standard.form.ZoomFormAction;

public class MagacinForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JLimitedTextField tfSifraPreduzeca = new JLimitedTextField(12, 12, true);
	private JLimitedTextField tfSifraSektora = new JLimitedTextField(12, 12, true);
	private JLimitedTextField tfSifraMagacina = new JLimitedTextField(12, 12, true);
	private JLimitedTextField tfNazivMagacina = new JLimitedTextField(30, 30, true);

	private JButton bSektorZoom;

	public MagacinForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		setTitle("Magacin");
		
		tableModel = new MagacinTableModel(new String[] {
				"Sifra preduzeca", "Sifra sektora", "Sifra magacina",
				"Naziv magacina" }, 0);
		
		tblGrid.setModel(tableModel);
		
		bSektorZoom = new JButton(new ZoomFormAction(this,
				new SektorForm()));
		
		children.add(new MagacinskaKarticaForm());
		children.add(new PopisniDokumentForm());
		children.add(new PrometniDokumentForm());
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblSifraPreduzeca = new JLabel("Sifra preduzeca:");
		JLabel lblSifraSektora = new JLabel("Sifra sektora:");
		JLabel lblSifraMagacina = new JLabel("Sifra magacina:");
		JLabel lblNazivMagacina = new JLabel("Naziv magacina:");

		dataPanel.add(lblSifraPreduzeca);
		dataPanel.add(tfSifraPreduzeca);
		dataPanel.add(bSektorZoom, "wrap");
		dataPanel.add(lblSifraSektora);
		dataPanel.add(tfSifraSektora, "pushx");
		dataPanel.add(lblSifraMagacina);
		dataPanel.add(tfSifraMagacina, "wrap");
		dataPanel.add(lblNazivMagacina);
		dataPanel.add(tfNazivMagacina, "pushx");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfSifraPreduzeca.setText("");
			tfSifraSektora.setText("");
			tfSifraMagacina.setText("");
			tfNazivMagacina.setText("");
			return;
		}
		String sifraPreduzeca = (String) tableModel.getValueAt(index, 0);
		String sifraSektora = (String) tableModel.getValueAt(index, 1);
		String sifraMagacina = (String) tableModel.getValueAt(index, 2);
		String nazivmagacina = (String) tableModel.getValueAt(index, 3);

		tfSifraPreduzeca.setText(sifraPreduzeca);
		tfSifraSektora.setText(sifraSektora);
		tfSifraMagacina.setText(sifraMagacina);
		tfNazivMagacina.setText(nazivmagacina);

		setMode(StandardForm.MODE_EDIT);

	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String nazivmagacina = tfNazivMagacina.getText().trim();
		try {
			int index = tableModel.insertRow(new String[] { sifraPreduzeca,
					sifraSektora, sifraMagacina, nazivmagacina });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}

	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String nazivmagacina = tfNazivMagacina.getText().trim();
		try {
			tableModel.searchRows(new String[] { sifraPreduzeca, sifraSektora,
					sifraMagacina, nazivmagacina });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String sifraPreduzeca = tfSifraPreduzeca.getText().trim();
		String sifraSektora = tfSifraSektora.getText().trim();
		String sifraMagacina = tfSifraMagacina.getText().trim();
		String nazivmagacina = tfNazivMagacina.getText().trim();
		try {
			int index = tableModel.editRow(new String[] { sifraPreduzeca,
					sifraSektora, sifraMagacina, nazivmagacina },
					tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfSifraPreduzeca.setText("");
		tfSifraSektora.setText("");
		tfSifraMagacina.setText("");
		tfNazivMagacina.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), 
				tfSifraMagacina.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		if(form instanceof SektorForm){
			tfSifraPreduzeca.setText(keys[0]);
			tfSifraSektora.setText(keys[1]);
		}
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		String[] retval = new String[]{};
		
		switch(index){
		case 0:{
			retval = new String[]{tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), 
					tfSifraMagacina.getText().trim(), "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
			break;
		}
		case 1:{
			retval = new String[]{"", "", tfSifraPreduzeca.getText().trim(), tfSifraSektora.getText().trim(), 
					tfSifraMagacina.getText().trim(), "", "", ""};
			break;
		}
		case 2:{
			retval = new String[]{"", tfSifraPreduzeca.getText().trim(), "", "", tfSifraSektora.getText().trim(), 
					tfSifraMagacina.getText().trim(), "", "", "", "", "", "", ""};
			break;
		}
		}
		
		return retval;
	}

}
